<!--TVM日结账单据录入-->
<template>
  <div class="TVMRiJieDanJuInput EditorOrAdd el-card is-always-shadow el-card__body">
    <el-dialog
      title="TVM日结账单据录入"
      :visible.sync="dialogVisible"
      @close="closed"
      width="950px">
      <avue-form ref="formEditor" v-if="isReload" class="TVMRiJieDanJuInputForm" :option="option" v-model="formData" @submit="handleSubmit" >
        <template slot-scope="scope" slot="lineCode">
          <el-select v-model="formData.lineCode" placeholder="请选择" style="width: 150px" clearable @change="changeLine">
            <el-option
              v-for="(item,index) in lineCodes"
              :key="index"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="stationCode">
          <el-select v-model="formData.stationCode" placeholder="请选择" style="width: 150px" @change="changeStation">
            <el-option
              v-for="(item,index) in stationCodes"
              :key="index"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="tvmCode">
          <el-select v-model="formData.tvmCode" placeholder="请选择" style="width: 150px" >
            <el-option
              v-for="(item,index) in tvmCodes"
              :key="index"
              :label="item.tvmCode"
              :value="item.tvmCode">
            </el-option>
          </el-select>
        </template>

        <template slot-scope="{row}" slot="listoption1">
          <avue-crud ref="listoption1" :option="listoption1" :data="formData.dynamic" @selection-change="(list)=>{selectedKeys=[];list.forEach(function(v,index) {
            selectedKeys.push(v.$index);
          })}">
            <template slot-scope="{row}" slot="receiveTotalMoney">
              <span>{{row.receiveTotalMoney=(Number(row.receiveNotes?row.receiveNotes:0)+Number(row.receiveCoin?row.receiveCoin:0)).toFixed(2)}}</span>
            </template>
            <template slot-scope="{row}" slot="changeTotalMoney">
              <span>{{row.changeTotalMoney=(Number(row.noteChangen?row.noteChangen:0)+Number(row.coinChange?row.coinChange:0)).toFixed(2)}}</span>
            </template>
            <template slot-scope="scope" slot="menuRight">
              <el-button type="primary" size="mini" @click="add(1)">新增</el-button>
              <el-button type="danger" size="mini" @click="del(1)">删除</el-button>
            </template>
          </avue-crud>
        </template>
        <template slot-scope="{row}" slot="listoption2">
          <avue-crud ref="listoption2" :option="listoption2" :data="formData.dynamic2" @selection-change="(list)=>{selectedKeys2=[];list.forEach(function(v,index) {
            selectedKeys2.push(v.$index);
          })}">
            <template slot-scope="scope" slot="menuRight">
              <el-button type="primary" size="mini" @click="add(2)">新增</el-button>
              <el-button type="danger" size="mini" @click="del(2)">删除</el-button>
            </template>
          </avue-crud>
        </template>

        <template slot-scope="scope" slot="menuForm" >
          <el-button type="primary" @click="closed">返回</el-button>
        </template>
      </avue-form>
    </el-dialog>
  </div>
</template>


<script>
  import {getForm, postForm} from '@/api/gen/gen'
  import {
    fenExYuangettoFixed,
    getgetStationCodes,
    getLineCodes,
    getSwPmDeviceOperationCodeList, getSwPmTvmCodeList,
    qianxiangType
  } from "../../../../../api/subwayRat/commonCheck";
  import {
    addSwStTvmDailyBills,
    getSwStTvmDailyBillCashPayList,
    getSwStTvmDailyBillElectronicPayList
  } from "../../../../../api/subwayRat/TVMRevenueVarianceAudit.TvmChaYiDXC/TVMRiJieDanJuInput";
 import {listoption1,listoption2} from './EditorOrAdd.js';
  export default {
    components:{
      listoption1,
      listoption2
    },
    data() {
      return {
        isReload:true,
        //1编辑，2添加，3预览
        showType:1,
        dialogVisible:true,
        lineCodes:[],
        stationCodes:[],
        selectedKeys:[], //新增行时使用
        selectedKeys2:[],//新增行时使用
        listoption1:{},
        listoption2:{},
        tvmCodes:[],
        formData:{
          dynamic: [],
          dynamic2: []
        },
        option:{
        },
        parth:null
    }
    },
    created() {


    },
    methods: {
      add(num){
        if(num==1){
          this.formData.dynamic.push({$cellEdit:true})
        }else if(num==2){
          this.formData.dynamic2.push({$cellEdit:true})
        }
      },
      del(num){
        if(num==1){
          for(let i=0;i<this.selectedKeys.length;i++){
            this.formData.dynamic.splice(this.selectedKeys[i],1);
          }
        }else if(num==2){
          for(let i=0;i<this.selectedKeys2.length;i++){
            this.formData.dynamic2.splice(this.selectedKeys[i],1);
          }
        }
      },
      async handleSubmit(form,done){
        let p={};
        Object.assign(p,this.formData);
        this.jisuan(p);
        let cashs=JSON.stringify(this.formData.dynamic);
        let electrons=JSON.stringify(this.formData.dynamic2);
        delete p.dynamic;
        delete p.dynamic2;
//        p.cashs=this.formatterAmountCashs(cashs,false);
//        p.electrons=this.formatterAmountElectrons(electrons,false);
        p.cashs=cashs
        p.electrons=electrons
        await addSwStTvmDailyBills(p).then(res=>{
          if(res.data.code==0){
            this.$message.success("操作成功！");
          }else{
            this.$message.error("操作失败！");
          }
        }).catch((e)=>{
          this.$message.error("操作失败！");
        });
        this.parth.getList();
        done();
        this.closed();
      },
      async recallShow(id,row,showType,parth){
        this.parth = parth;
        this.showType=showType;
        this.getOption();
        if(showType==2){//添加
          return;
        }
        this.formData={dynamic:[],dynamic2:[]}
       Object.assign(this.formData,row);
        let th=this;
        //获取现金数据
        let res = await getSwStTvmDailyBillCashPayList({relationId:id});
        debugger
        th.formData.dynamic=res.data.data; //
        th.formData.dynamic.forEach(function (v) {
          v.$cellEdit=true;
        })
//        this.formatterAmountCashs(th.formData.dynamic,true);
        //获取电子数据
        let res2 = await getSwStTvmDailyBillElectronicPayList({relationId:id});
        th.formData.dynamic2 = res2.data.data;
        th.formData.dynamic2.forEach(function (v) {
          v.$cellEdit=true;
        });
//        this.formatterAmountElectrons(th.formData.dynamic2,true);
      },
      formatterAmountCashs(rows,isYuan){ //格式化钱
        rows = JSON.parse(rows);
        if(isYuan){
          for(var i=0;i<rows.length;i++){
            var row=rows[i];
            row.receiveNotes=fenExYuangettoFixed(row,'receiveNotes');
            row.receiveCoin=fenExYuangettoFixed(row,'receiveCoin');
            row.noteChangen=fenExYuangettoFixed(row,'noteChangen');
            row.faultRefundAmount=fenExYuangettoFixed(row,'faultRefundAmount');
            row.receiveTotalMoney=fenExYuangettoFixed(row,'receiveTotalMoney');
            row.sellTicketTotalMoney=fenExYuangettoFixed(row,'sellTicketTotalMoney');
            row.rechargeMoney=fenExYuangettoFixed(row,'rechargeMoney');
            row.changeTotalMoney=fenExYuangettoFixed(row,'changeTotalMoney');
            row.coinChange=fenExYuangettoFixed(row,'coinChange');
          }
        }else if(!isYuan){
          for(var i=0;i<rows.length;i++){
            var row=rows[i];
            row.receiveNotes=Number(row.receiveNotes)*100;
            row.receiveCoin=Number(row.receiveCoin)*100;
            row.noteChangen=Number(row.noteChangen)*100;
            row.faultRefundAmount=Number(row.faultRefundAmount)*100;
            row.receiveTotalMoney=Number(row.receiveTotalMoney)*100;
            row.sellTicketTotalMoney=Number(row.sellTicketTotalMoney)*100;
            row.rechargeMoney=Number(row.rechargeMoney)*100;
            row.changeTotalMoney=Number(row.changeTotalMoney)*100;
            row.coinChange=Number(row.coinChange)*100;
          }
        }
        return JSON.stringify(rows);
      },
      formatterAmountElectrons(rows,isYuan){ //格式化钱
        rows = JSON.parse(rows);
        if(isYuan){
          for(var i=0;i<rows.length;i++){
            var row=rows[i];
            row.faultRefundAmount=fenExYuangettoFixed(row,'faultRefundAmount');
            row.rechargeMoney=fenExYuangettoFixed(row,'rechargeMoney');
            row.sellTicketMoney=fenExYuangettoFixed(row,'sellTicketMoney');
          }
        }else if(!isYuan){
          for(var i=0;i<rows.length;i++){
            var row=rows[i];
            row.faultRefundAmount=Number(row.faultRefundAmount)*100;
            row.rechargeMoney=Number(row.rechargeMoney)*100;
            row.sellTicketMoney=Number(row.sellTicketMoney)*100;
          }
        }
        return JSON.stringify(rows);
      },
      closed(){
        this.dialogVisible=false;
        this.parth.isShowDia=false;
      },
      getOption(){
        let th=this;
        this.option={
          submitBtn:this.showType!=3,
          emptyBtn:false,
          group:[
            {
              icon:'',
              label: '',
              // collapse:false,
              prop: 'group1',
              column: [{
                label: '线路名称',
                prop: 'lineCode',
                type: 'select',
                labelWidth:130,
                formslot:true,
                disabled: this.showType==3
              },{
                label: '站点名称',
                prop: 'stationCode',
                type: 'select',
                // width:250,
                labelWidth:130,
                formslot:true,
                disabled: this.showType==3
              },{
                label: '开始时间',
                prop: 'startTime',
                valueFormat: 'yyyy-MM-dd',
                // width:'250',
                labelWidth:130,
                type:'date',
                disabled: this.showType==3
              },{
                label: '结束时间',
                prop: 'endTime',
                valueFormat: 'yyyy-MM-dd',
                // width:'250',
                labelWidth:130,
                type:'date',
                disabled: this.showType==3
              },
                {label: "tvm编号", prop: "tvmCode", type: 'select', formslot: true}
                ]
            },
            {
              icon:'',
              sortable:true,
              label: '现金支付',
              // collapse:false,
              width:600,
              span:24,
              prop: 'group2',
              column: [{
                label: '',
                prop: 'listoption1',//列表展示使用
                formslot:true,
              },
              ]
            },
            {
              icon:'',
              sortable:true,
              label: '电子支付',
              // collapse:false,
              width:600,
              span:24,
              prop: 'group3',
              column: [{
                label: '',
                prop: 'listoption2',//列表展示使用
                formslot:true,
              }
              ]
            }
          ]
        };
        this.listoption1=listoption1(th); //加载列表
        this.listoption2=listoption2(th);//加载列表
        this.isReload=false;
        this.$nextTick(()=>{
          this.isReload=true;
        })
      },
      jisuan(p){
        let cashs=p.dynamic;
        let el=p.dynamic2;
        let dailyCashTotalAmount=0;
        let electronicPayTotalAmount=0;

        for(let i=0;i<cashs.length;i++){
          dailyCashTotalAmount+=(cashs[i].receiveNotes+cashs[i].receiveCoin);
        }
        for(let i=0;i<el.length;i++){
          electronicPayTotalAmount+=(el[i].faultRefundAmount+el[i].rechargeMoney);
        }
        p.dailyCashTotalAmount=dailyCashTotalAmount;
        p.electronicPayTotalAmount=electronicPayTotalAmount;
      },
      changeLine(v){ //改变线路时
        getgetStationCodes(v).then(res=>{
          this.stationCodes=res;
        })
      },
      changeStation(v){ //改变车站
        let param={};
        param.lineCode=this.formData.lineCode;
        param.stationCode=v;
        getSwPmTvmCodeList(param).then(r=>{
          this.tvmCodes=r.data;
        });
      },
      clearForm(){
        this.formData={dynamic:[],dynamic2: []};
      }
    },
    mounted() {
      getLineCodes().then(res=>{ //获取线路
        this.lineCodes=res;
      });
    }
  }
</script>
<style lang="scss">
  .TVMRiJieDanJuInput.EditorOrAdd{
    .avue-group[index='2']{
      .avue-form__group{
        .avue-form__row{
          width: 100%;
        }
      }
    }
    .avue-group[index='3']{
      .avue-form__group{
        .avue-form__row{
          width: 100%;
        }
      }
    }

    .avue-crud__tip{
      display: none;
    }
  }

</style>
